package com.jcp.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jcp.entity.FalseNote;
import com.jcp.entity.Requests;
import com.jcp.entity.UsersLogin;
import com.jcp.serviceimp.RequestsServiceImp;
import com.jcp.util.LayuiData;
import com.jcp.util.PageData;

/**
 * Servlet implementation class RequestsServlet
 */
@WebServlet("/RequestsServlet")
public class RequestsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	RequestsServiceImp requestsServiceImp = new RequestsServiceImp();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RequestsServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置字符集编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		doQurry(request, response);
		String op = "";

		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		//删除请假信息
		if (op.equals("del")) {
			System.out.println("标记");
			if (request.getParameter("id") != null) {

				String id = request.getParameter("id");

				boolean flagDel = requestsServiceImp.del(Integer.parseInt(id));

				PrintWriter out = response.getWriter();
				out.print(flagDel);
				out.close();

			}
			doQurry(request, response);
			//同意请假
		} else if (op.equals("agree")) {

			if (request.getParameter("id") != null) {

				String reqId = request.getParameter("id");

				boolean flagagree = requestsServiceImp.update("批准", Integer.parseInt(reqId));

				PrintWriter out = response.getWriter();
				out.print(flagagree);
				out.close();

			}
			doQurry(request, response);
			//拒绝请假
		} else if (op.equals("refuse")) {
			
			if (request.getParameter("id") != null) {

				String reqId = request.getParameter("id");

				boolean flagRefuse = requestsServiceImp.update("拒绝", Integer.parseInt(reqId));

				PrintWriter out = response.getWriter();
				out.print(flagRefuse);
				out.close();

			}
			doQurry(request, response);
		}

	}

	private void doQurry(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 设置预期响应的类型
		response.setContentType("application/json");
		// 创建printwriter
		PrintWriter out = response.getWriter();

		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		int limit = 10;
		if (request.getParameter("limit") != null) {
			limit = Integer.parseInt(request.getParameter("limit"));
		}

		String strLike = "";
		if (request.getParameter("strLike") != null) {
			strLike = request.getParameter("strLike");
		}
		// 下面的语句应该改为userId
		int userId=1;
		if (request.getSession().getAttribute("user") != null) {
			UsersLogin user = (UsersLogin) request.getSession().getAttribute("user");
			// fromUserId=user.getUserId();
			userId = user.getUserId();

		}

		PageData<FalseNote> pd = requestsServiceImp.getFalseNoteByPage(page, limit, userId);
		LayuiData<FalseNote> layuiData = new LayuiData<>(0, "", pd.getTotalCount(), pd.getData());
        System.out.println(pd);
		String returnvalue = new Gson().toJson(layuiData);
		
		out.print(returnvalue);

		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 1请求和响应编码[过滤器]
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		doGet(request, response);
	}

}
